package com.cpimex.eprice;
import java.util.Properties;
import java.util.Vector;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


/**
 * 
 * 
 *
 */

public class Mailer {

	private String body;
	private String subject;
	private String passwordFrom;
	private String mailFrom;
	private String mailAut;
	private Vector mailTo;
	private Vector mailCC;
	private String host;
	private boolean authentication;

	/**
	 * @param authentication the authentication to set
	 */
	public void setAuthentication(boolean authentication) {
		this.authentication = authentication;
	}




	/**
	 * @param body the body to set
	 */
	public void setBody(String body) {
		this.body = body;
	}




	/**
	 * @param host the host to set
	 */
	public void setHost(String host) {
		this.host = host;
	}




	/**
	 * @param mailAut the mailAut to set
	 */
	public void setMailAut(String mailAut) {
		this.mailAut = mailAut;
	}




	/**
	 * @param mailCC the mailCC to set
	 */
	public void setMailCC(Vector mailCC) {
		this.mailCC = mailCC;
	}




	/**
	 * @param mailFrom the mailFrom to set
	 */
	public void setMailFrom(String mailFrom) {
		this.mailFrom = mailFrom;
	}




	/**
	 * @param mailTo the mailTo to set
	 */
	public void setMailTo(Vector mailTo) {
		this.mailTo = mailTo;
	}




	/**
	 * @param passwordFrom the passwordFrom to set
	 */
	public void setPasswordFrom(String passwordFrom) {
		this.passwordFrom = passwordFrom;
	}




	/**
	 * @param subject the subject to set
	 */
	public void setSubject(String subject) {
		this.subject = subject;
	}




	public static void main(String[] args) throws Exception {
		Mailer m = new Mailer();

		Vector v =new Vector();
		v.add("luiscelis@yahoo.com");

		m.setAuthentication(false);
		m.setHost("127.0.0.1");
		m.setMailFrom("Luis");
		m.setBody("     a");
		m.setPasswordFrom(" q");
		m.setSubject("q");
		m.setMailTo(v);
		m.sendMail();
	}

	public String sendMail() throws Exception{
		try{
			if( body != null && subject != null && passwordFrom != null && mailFrom!=null && mailTo != null && host != null && !body.equals("") && !subject.equals("") && !passwordFrom.equals("") && !mailFrom.equals("") && !mailTo.equals("") && !host.equals("") ){
				//System.out.println("Send");
				// Get system properties
				Properties props = System.getProperties();
				// Setup mail server
				props.put("mail.smtp.host", host);
				props.put("mail.smtp.auth", new Boolean( authentication ).toString() );
				// Get session
				Session sesion;
				if( authentication ){
					sesion = Session.getInstance(props, new Authenticator() {
						public PasswordAuthentication getPasswordAuthentication() {
							return new PasswordAuthentication( mailAut, passwordFrom );
						}
					}
					);
				}else{
					sesion = null;
				}
				// Define message
				Message message = new MimeMessage(sesion);
				message.setHeader("Content-Type", "text/html");
				message.setFrom(new InternetAddress(mailFrom +"<"+mailFrom+">"));
				InternetAddress[] ReplyAddress = {new InternetAddress(mailFrom)};
				message.setReplyTo(ReplyAddress);
				if( mailTo != null ){
					for( int i = 0; i < mailTo.size(); i++ ){
						message.addRecipient(Message.RecipientType.TO,
								new InternetAddress((String)mailTo.get(i)));
					}
				}
				if( mailCC != null ){
					for( int i = 0; i < mailCC.size(); i++ ){
						message.addRecipient(Message.RecipientType.CC,
								new InternetAddress((String)mailCC.get(i)));
					}
				}
				message.setSubject(subject);
				// Create the message part
				BodyPart messageBodyPart = new MimeBodyPart();
				// Fill the message
				messageBodyPart.setText(body);
				//Set the content type of message
				messageBodyPart.setHeader("Content-Type", "text/html");
				// Create a Multipart
				Multipart multipart = new MimeMultipart();
				// Add part one
				multipart.addBodyPart(messageBodyPart);
				// Put parts in message
				message.setContent(multipart);
				// Send the message
				
				Transport.send(message);
				return "ok";
			
			}else{
			return "Error: Faltan Datos Para Enviar Mail";
			}
		}catch( Exception ex ){
			
			throw ex;
			
		}
	}
}